package activity;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.zou.screenfilter.R;

public class HomeActivity extends Activity {
	private WindowManager mWindowManager;

	private WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
	private View toastView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		createWindowManager();
		// showDesk(80);
	}

	/**
	 * 设置WindowManager
	 */
	private void createWindowManager() {
		// 取得系统窗体
		mWindowManager = (WindowManager) getApplicationContext().getSystemService("window");

		// 窗体的布局样式
		WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams();

		// 设置窗体显示类型——TYPE_SYSTEM_ALERT(系统提示)
		mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

		// 设置窗体焦点及触摸：
		// FLAG_NOT_FOCUSABLE(不能获得按键输入焦点)
		mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

		// 设置显示的模式
		mLayoutParams.format = PixelFormat.RGBA_8888;

		// 设置对齐的方法
		mLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;

		// 设置窗体宽度和高度
		mLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
		mLayoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;

		// 设置窗体显示的位置，否则在屏幕中心显示
		mLayoutParams.x = 0;
		mLayoutParams.y = 0;
	}

	private void showDesk(int paramInt) {
		toastView = ((LayoutInflater) getSystemService("layout_inflater")).inflate(R.layout.main, null);
		toastView.setFocusable(false);
		toastView.setClickable(false);
		toastView.setKeepScreenOn(false);
		toastView.setLongClickable(false);
		toastView.setFocusableInTouchMode(false);
		toastView.setBackgroundDrawable(getBackgroundDrawable(paramInt));

		localLayoutParams = new WindowManager.LayoutParams();
		localLayoutParams.height = -1;
		localLayoutParams.width = -1;
		localLayoutParams.flags = 280;
		localLayoutParams.format = -3;
		localLayoutParams.type = 2006;
		localLayoutParams.x = 0;
		localLayoutParams.y = 0;
		localLayoutParams.verticalWeight = 1.0F;
		localLayoutParams.horizontalWeight = 1.0F;
		localLayoutParams.verticalMargin = 0.0F;
		localLayoutParams.horizontalMargin = 0.0F;

		mWindowManager.addView(toastView, localLayoutParams);
		finish();
	}

	private static int computeAlpha(int paramInt) {
		return (int) Math.round(255.0D * Math.exp(4.0D * (paramInt / 100.0D) - 4.0D));
	}

	private Drawable getBackgroundDrawable(int paramInt) {
		int i = 255 - computeAlpha(paramInt);
		Log.i("Screen Filter", "Background alpha=" + i);
		return new ColorDrawable(Color.argb(i, 0, 0, 0));
	}

}
